<template>
  <div style="height:100%;width:100%">
    <el-row class="mainContent" >
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true"  :model="filters" size="small" style="text-align:left;" ref="filters">
          <el-form-item prop="id">
            <el-input v-model="filters.roleName" placeholder="角色名称" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <div class="block">
              <el-date-picker
                v-model="filters.begtime"
                type="datetime" placeholder="选择日期" >
              </el-date-picker>
              <el-date-picker
                v-model="filters.endtime"
                type="datetime" placeholder="选择日期" >
              </el-date-picker>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  @click="getUsers">查询</el-button>
          </el-form-item>
          <el-form-item v-if="$store.state.routes.routes.xtRoleAdd">
            <el-button type="primary"  @click="add">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <!--列表-->
      <el-table :data="users" highlight-current-row v-loading="listLoading" style="width: 100%;">
        <el-table-column prop="roleId" label="角色ID"  >
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称"  >
        </el-table-column>
        <el-table-column prop="creDate" label="创建时间"  >
        </el-table-column>
        <el-table-column  label="操作" v-if="$store.state.routes.routes.xtRoleDel || $store.state.routes.routes.xtRoleEdit || $store.state.routes.routes.xtRoleToAccess">
          <template slot-scope="scope">
            <el-button v-if="$store.state.routes.routes.xtRoleDel"
              size="mini"
              type="danger"
              @click="delAccess(scope.$index, scope.row)">删除</el-button>
            <el-button v-if="$store.state.routes.routes.xtRoleEdit"
              size="mini"
              type="primary"
              @click="edit(scope.$index, scope.row)">编辑</el-button>
            <el-button v-if="$store.state.routes.routes.xtRoleToAccess"
              size="mini"
              type="warning"
              @click="$router.push({path:'xitong_roleAccessDetail',query:{id:scope.row.roleId,name:scope.row.roleName,from:'xitong_role'}})">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="limit"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-row>
    <el-dialog title="新增角色" :visible.sync="addUsersVisible" width="30%" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="addForm" ref="addForm" status-icon label-width="100px" :rules="addFormRules" style="text-align:left">
        <el-form-item label="角色名称" prop="roleName">
          <el-col :span="22">
            <el-input v-model="addForm.roleName"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addUsersVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="submitAdd('addForm')" :loading="addLoading">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑角色" :visible.sync="editUsersVisible" width="30%" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="editForm" ref="addForm" status-icon label-width="100px" :rules="editFormRules" style="text-align:left">
        <el-form-item label="角色ID"  style="display: none">
          <el-col :span="22">
            <el-input v-model="editForm.roleId"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleName">
          <el-col :span="22">
            <el-input v-model="editForm.roleName"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editUsersVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="submitEdit('addForm')" :loading="editLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script>
  import {datetime_to_unix} from '@/assets/js/date.js'
  export default {
    data() {
      return {
        filters:{
          roleName:'',
          begtime:'',
          endtime:''
        },
        addForm:{},
        editForm:{},
        listLoading:false,
        addLoading:false,
        editLoading:false,
        addUsersVisible:false,
        editUsersVisible:false,
        limit:10,
        page:1,
        total:0,
        users:[],
        addFormRules:{
          roleName: [
            { required: true, message: '请输入角色名称', trigger: 'blur' }]
        },
        editFormRules: {
          permissionName: [
            {required: true, message: '请输入角色名称', trigger: 'blur'}],
        }
      };
    },
    created() {
      this.getUsers()
    },
    methods: {
      getUsers() {
        this.users = []
        this.listLoading = true;
        var obj = {
          data:{
            roleName:this.filters.roleName,
            begtime:this.filters.begtime==''||this.filters.begtime==0||this.filters.begtime==null?'':Math.round(new Date(this.filters.begtime).getTime()/1000),
            endtime:this.filters.endtime==''||this.filters.endtime==0||this.filters.endtime==null?'':Math.round(new Date(this.filters.endtime).getTime()/1000),
            pageNo:this.page
          }
        };
        this.$http('sys/role/list.do',obj).then(resp => {
          if(resp.code==1){
            this.users = resp.data.list;
            this.total = resp.data.total;
          }
          this.listLoading = false;
        });
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getUsers();
      },
      delAccess(index,row){
        if(!this.$store.state.routes.routes.xtRoleDel){
          this.$message({
            message:'无权限',
            type:'error'
          })
          return
        }
        this.$confirm("确认删除该角色么?", "提示", {})
          .then(() => {
            this.$http('sys/role/del.do',{data:{roleId:row.roleId}}).then(resp=>{
              if(resp.code==1){
                this.$message({
                  message:'删除成功',
                  type:'success'
                })
              }else{
                this.$message({
                  message:resp.msg,
                  type:'error'
                })
              }
              this.getUsers()
            })
          })
          .catch(() => {});
      },
      add(){
        if(!this.$store.state.routes.routes.xtRoleAdd){
          this.$message({
            message:'无权限',
            type:'error'
          })
          return
        }
        this.addForm = {}
        this.addUsersVisible = true
      },
      submitAdd(fn){

        this.$refs[fn].validate((valid) => {
          if (valid) {
            this.addLoading = true;
            var obj = {
              data:this.addForm
            }
            this.$http('sys/role/add.do',obj).then(resp => {
              if(resp.code==1){
                this.$message({
                  message:'新建成功',
                  type:'success'
                })
              }else{
                this.$message({
                  message:resp.msg,
                  type:'error'
                })
              }
              this.getUsers()
              this.addLoading = false;
              this.addUsersVisible = false;
            });

          } else {
            return false;
          }
        });
      },
      edit(index,row){
        if(!this.$store.state.routes.routes.xtRoleEdit){
          this.$message({
            message:'无权限',
            type:'error'
          })
          return
        }
        this.editForm.roleId = row.roleId
        this.editForm.roleName = row.roleName
        this.editUsersVisible = true;
      },
      submitEdit(fn){
        this.$refs[fn].validate((valid) => {
          if (valid) {
            this.editLoading = true;
            var obj = {
              data:this.editForm
            }
            this.$http('sys/role/modify.do',obj).then(resp => {
              if(resp.code==1){
                this.$message({
                  message:'修改成功',
                  type:'success'
                })
              }else{
                this.$message({
                  message:resp.msg,
                  type:'error'
                })
              }
              this.getUsers()
              this.editLoading = false;
              this.editUsersVisible = false;
            });

          } else {
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped>
  .el-input__inner{
    width: 180px !important;
  }
  .absoSpan{
    position: absolute;
    left: -11px;
  }
</style>
